class Solution:
    def maxOperations(self, nums: list, k: int) -> int:
        numMap = {}
        for i in range(len(nums)):
            if nums[i] not in numMap:
                numMap[nums[i]] = []
            numMap[nums[i]] += [i]
        res = 0
        for num in nums:
            if len(numMap[num]) == 0:
                continue
            numMap[num].pop(0)
            diff = k - num
            if diff not in numMap or len(numMap[diff]) == 0:
                continue
            numMap[diff].pop(0)
            res += 1

        return res
